home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
USEFUL_C
/
DRIVER.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-04-29
|
5KB
|
259 lines
/****************************************************************************
*
* FILE: driver.c
* CREA: Sven Axelsson, GU
* MODF: tisdag 12 december 1989 @ 23.33.48
* HIST: 89-12-12 (1.0) First version.
*
****************************************************************************/
/****************************************************************************
This is basically the same code that appeared in Dr. Dobb's Journal,
Macintosh Issue of Fall 1989, in an article on device drivers by
Bryan Waters.
I have found it extremely convenient to use this when writing DAs and
such, so here is the code for those who can't be bothered with typing
in themselves.
I have added the global gRsrcBase for convenience, so that you easily
can get the DAs owned resources from anywhere in your code without having
to pass variables or objects around. It is also avaliable as the object
variable rsrcBase for those who preferres it that way.
****************************************************************************/
# include "driver.h"
# define OPEN 0
# define PRIME 1
# define CONTROL 2
# define STATUS 3
# define CLOSE 4
driver *gDrvr = NULL; /* The driver object */
short gRsrcBase; /* Add this offset for owned resources */
/******************************************************************************
main
******************************************************************************/
short
main(
CntrlParam *paramBlock,
DCtlPtr devCtlEnt,
short routine )
{
short result = 0;
if( routine != OPEN && gDrvr != NULL ) {
gDrvr->pb = paramBlock;
gDrvr->dc = devCtlEnt;
gDrvr->async = paramBlock->ioTrap & asyncTrpBit;
gDrvr->status = 0;
}
else if( routine != OPEN )
return badUnitErr;
switch( routine ) {
case OPEN:
if( gDrvr == NULL )
if( devCtlEnt->dCtlStorage == NULL )
result = -1;
else {
gDrvr = New();
if( gDrvr == NULL )
result = -1;
else {
gDrvr->pb = paramBlock;
gDrvr->dc = devCtlEnt;
gDrvr->async = paramBlock->ioTrap & asyncTrpBit;
gDrvr->drvrRef = devCtlEnt->dCtlRefNum;
gDrvr->rsrcBase = 0xC000 | (~gDrvr->drvrRef << 5);
gRsrcBase = gDrvr->rsrcBase;
gDrvr->Open();
}
}
break;
case PRIME:
gDrvr->Prime();
break;
case CONTROL:
gDrvr->Control();
break;
case STATUS:
gDrvr->Status();
break;
case CLOSE:
gDrvr->Close();
result = gDrvr->status;
delete( gDrvr );
gDrvr = NULL;
break;
}
if( gDrvr != NULL )
result = gDrvr->status;
return( result );
}
/******************************************************************************
Open - METHOD
******************************************************************************/
void
driver::Open( void )
{
status = 0;
}
/******************************************************************************
Prime - METHOD
******************************************************************************/
void
driver::Prime( void )
{
if( pb->ioTrap & 0x00FF == aRdCmd )
Read();
else
Write();
}
/******************************************************************************
Read - METHOD
******************************************************************************/
void
driver::Read( void )
{
}
/******************************************************************************
Write - METHOD
******************************************************************************/
void
driver::Write( void )
{
}
/******************************************************************************
Control - METHOD
******************************************************************************/
void
driver::Control( void )
{
switch( pb->csCode ) {
case accRun:
Idle();
break;
case goodbye:
Close();
break;
}
}
/******************************************************************************
Status - METHOD
******************************************************************************/
void
driver::Status( void )
{
status = 0;
}
/******************************************************************************
Idle - METHOD
******************************************************************************/
void
driver::Idle( void )
{
}
/******************************************************************************
Close - METHOD
******************************************************************************/
void
driver::Close( void )
{
status = noErr;
}
/******************************************************************************
Error - METHOD
******************************************************************************/
void
driver::Error(
OSErr err )
{
status = err;
}
/******************************************************************************
Fetch
******************************************************************************/
char
Fetch(
DCtlPtr dc,
short *lastChar )
{
short data;
asm
{
move.l dc, a1
move.l jFetch, a0
jsr (a0)
move.b d0, data
}
*lastChar = data & 0x8000;
return( data & 0x00FF );
}
/******************************************************************************
Stash
******************************************************************************/
short
Stash(
DCtlPtr dc,
char charRead )
{
short data;
asm
{
move.l dc, a1
move.b charRead, d0
move.l jStash, a0
jsr (a0)
move.b d0, data
}
return( data & 0x8000 );
}